package com.crm.customer.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.crm.customer.model.Customer;
import com.service.CustomerService;

public class CustomerEditController extends HttpServlet {
 /**
	 * 
	 */
	private static final long serialVersionUID = -5091789489370823517L;

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
	 	 
	 String customer_id = req.getParameter("CUSTOMER_ID");
	 CustomerService model = new CustomerService();
	 Customer customer = model.findSingleCustomer(customer_id);
	 req.setAttribute("customer", customer);
	 req.getRequestDispatcher("/View/CustomerUpdate.jsp").forward(req, resp);
	 
 }

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
	
	CustomerService model = new CustomerService();
	Customer customer = new Customer(req.getParameter("customer_id"),
			                            req.getParameter("firstName"),
			                            req.getParameter("lastName"),
			                            req.getParameter("sex"),
	                                    req.getParameter("package_id"),
	                                    req.getParameter("employee_id"),
	                                    Integer.parseInt(req.getParameter("age")),	                                   
	                                    Integer.parseInt(req.getParameter("height")),
	                                    Integer.parseInt(req.getParameter("weight")));
	boolean result = model.editCustomer(customer);
	if(result) req.getRequestDispatcher("/View/CustomerUpdateSuccess.jsp").forward(req,resp);
	else req.getRequestDispatcher("/View/CustomerUpdateFailed.jsp").forward(req,resp);
}
}
